// TFTLCD Define

#define 	PORT_LB      	PORTA
#define 	PORT_HB      	PORTC

#define     DDR_LB          DDRA
#define     DDR_LH          DDRC

#define 	PORT_NCS	PORTE.3			
#define 	PORT_RS	    PORTE.5			
#define 	PORT_NWR	PORTE.6
#define 	PORT_NRST	PORTE.4
#define 	PORT_NRD	PORTE.2
#define 	PORT_NLED	PORTE.7

#define 	DDR_NCS	    DDRE.3			
#define 	DDR_RS	    DDRE.5			
#define 	DDR_NWR	    DDRE.6
#define 	DDR_NRST	DDRE.4
#define 	DDR_NRD	    DDRE.2
#define 	DDR_NLED	DDRE.7  

#define         H_MAX           240 
#define         V_MAX           320

void WriteI(unsigned char wric);
void WriteD(unsigned char wrid);
void Init_Data(unsigned char wric , unsigned char wrid);
void Init_TFTLCD (void);
void PartialMode(void);
void DrawBlock (unsigned int startx,unsigned int starty,unsigned int endx,unsigned int endy,unsigned int color1,unsigned char color2);
void ResetTFTLCD (void);
void PortInitTFTLCD (void);
void InitTFTLCD (void);


void WriteI(unsigned char wric)
{  
 
	 PORT_RS = 0 ;
 	 PORT_NCS = 1 ;
	 PORT_NWR = 1 ;
 	 PORT_LB = wric ;
	 PORT_NCS = 0 ;
	 PORT_NWR = 0 ;
	 PORT_NWR = 1 ;
	 PORT_NCS = 1 ;
   
}


void  WriteD(unsigned char wrid)
{ 
	 PORT_RS = 1;
	 PORT_NCS = 1;
	 PORT_NWR = 1;
	 PORT_LB = wrid;
	 PORT_NCS = 0;
	 PORT_NWR = 0;
	 PORT_NWR = 1;
	 PORT_NCS = 1;
}

void  Write_data(u08 data1, u08 data2)
{       

        WriteI(0x0022);
 
	 PORT_RS =1; //sbi(RS_PORT, RS);
	 PORT_NCS = 0 ; // cbi(NCS_PORT, NCS);
	 PORT_LB =  data1;
	 PORT_HB = data2; 
	
	 PORT_NWR =0; //cbi(NWR_PORT, NWR);
	 PORT_NWR =1; //sbi(NWR_PORT, NWR);
	
	 PORT_NCS = 1; //sbi(NCS_PORT, NCS);
}


void Init_Data(unsigned char wric , unsigned char wrid)
{
	WriteI(wric);
	WriteD(wrid);
}


void Init_TFTLCD (void) 
{
        //Gamma setting 
 	Init_Data(0x0046,0x0081);
 	Init_Data(0x0047,0x0010);
 	Init_Data(0x0048,0x0001);
	Init_Data(0x0049,0x0004);
	Init_Data(0x004A,0x0010);
	Init_Data(0x004B,0x0076);
	Init_Data(0x004C,0x0037);
	Init_Data(0x004D,0x0067);
	Init_Data(0x004E,0x0007);
	Init_Data(0x004F,0x0014);
	Init_Data(0x0050,0x000A);
	Init_Data(0x0051,0x000E);

        //Window Setting

   	Init_Data(0x0002,0x0000);
	Init_Data(0x0003,0x0000);
	Init_Data(0x0004,0x0000);
	Init_Data(0x0005,0x00EF);
	Init_Data(0x0006,0x0000);
	Init_Data(0x0007,0x0000);
	Init_Data(0x0008,0x0001);
	Init_Data(0x0009,0x003F);

        //Display Setting
	delay_ms(10);


	Init_Data(0x0001,0x0006);

	//Delayms(10);
	
	Init_Data(0x0016,0x00C8);
	Init_Data(0x0023,0x0095);
	Init_Data(0x0024,0x0095);
	Init_Data(0x0025,0x00FF);
	Init_Data(0x0027,0x0002);
	Init_Data(0x0028,0x0002);
	Init_Data(0x0029,0x0002);
	Init_Data(0x002A,0x0002);
	Init_Data(0x002C,0x0002);
	Init_Data(0x002D,0x0002);

	Init_Data(0x003A,0x0001);
	Init_Data(0x003B,0x0001);
	Init_Data(0x003C,0x00F0);
	Init_Data(0x003D,0x0000);

	delay_ms(20);

	Init_Data(0x0035,0x0038);
	Init_Data(0x0036,0x0078);
	Init_Data(0x003E,0x0038);
	Init_Data(0x0040,0x000F);
	Init_Data(0x0041,0x00F0); 

	Init_Data(0x0038,0x0000);

        // Power Setting

 	Init_Data(0x0019,0x0049);
	Init_Data(0x0093,0x000A);

	delay_ms(10);

	Init_Data(0x0020,0x0050);
	Init_Data(0x001D,0x0004);
	Init_Data(0x001E,0x0001);

	Init_Data(0x001F,0x0008);
	Init_Data(0x0044,0x0054); 
	Init_Data(0x0045,0x0010);

	delay_ms(10); 

	Init_Data(0x001C,0x0004); 

	delay_ms(20);

	Init_Data(0x0043,0x0080);  

	delay_ms(5);

	Init_Data(0x001B,0x0008);    

	delay_ms(40); 

	Init_Data(0x001B,0x0010);      

	delay_ms(40);  

        //Display On Setting

	Init_Data(0x0090,0x007F);
	Init_Data(0x0026,0x0004);
 
	delay_ms(40); 

	Init_Data(0x0026,0x0024);
	Init_Data(0x0026,0x002C);
 
	delay_ms(40);  

	Init_Data(0x0026,0x003C); 
  	Init_Data(0x0057,0x0002);
	Init_Data(0x0055,0x0000);
	Init_Data(0x0057,0x0000);
}

void PartialMode(void)
{
	Init_Data(0x0001,0x0005) ;	
	Init_Data(0x000A,0x0000) ;	
	Init_Data(0x000B,0x000F) ;	
	Init_Data(0x000C,0x0001) ; 	
	Init_Data(0x000D,0x000F) ;
}


void DrawBlock (unsigned int startx,unsigned int starty,unsigned int endx,unsigned int endy,unsigned int color1,unsigned char color2)
{
        int i,j;
  
        if(endx>(H_MAX-1))
                endx=(H_MAX-1);
        if(endy>(V_MAX-1))
                endy=(V_MAX-1);
        if(startx>endx)
                startx=endx;
        if(starty>endy)
                starty=endy;
 

        Init_Data(0x02,startx/256);
        Init_Data(0x03,startx);
        Init_Data(0x04,endx/256);
        Init_Data(0x05,endx);
        Init_Data(0x06,starty/256);
        Init_Data(0x07,starty);
        Init_Data(0x08,endy/256);
        Init_Data(0x09,endy); 


	WriteI(0x22);

	PORT_RS = 1 ;
        PORT_NCS = 0;
        PORT_LB = color1 ;
  	PORT_HB = color2 ;
    
        for(i=starty;i<=endy;i++)
   	{
     		for(j=startx;j<=endx;j++)
	   	{  
	   		 PORT_NWR = 0 ;     
	   		 PORT_NWR = 1 ;	
	  	 }
   	}
   	 
        PORT_NCS = 1;
}

void ResetTFTLCD (void)
{
        PORT_NRST = 0;
        delay_ms(1);
        PORT_NRST = 1;
        delay_ms(15);           
}


void PortInitTFTLCD (void)
{
        DDR_LB = 0xff;
        DDR_LH = 0xff;
        PORT_LB = 0xff;
        PORT_LB = 0xff; 
        

  
        
        DDR_NCS	= 1;	
        DDR_RS = 1;		
        DDR_NWR = 1;
        DDR_NRST = 1;
        DDR_NRD = 1;
        DDR_NLED = 1;

        PORT_NCS = 1;	
        PORT_RS = 1;		
        PORT_NWR = 1;
        PORT_NRST = 1;
        PORT_NRD = 1;
        PORT_NLED = 1;
}

void InitTFTLCD (void)
{
        PortInitTFTLCD();
        
        PORT_NLED = 0;
        
        ResetTFTLCD();     
        Init_TFTLCD (); 
       // PartialMode();



}


void tftlcdSetPixel (u16 startx,u16 starty, u16 color)
{
	 if(startx>(H_MAX-1))
	  startx = (H_MAX-1);

	 if(starty>(V_MAX-1))
	  starty = (V_MAX-1);

	 Init_Data(0x0002,startx >> 8);
	 Init_Data(0x0003,startx);
	 Init_Data(0x0004,startx >> 8);
	 Init_Data(0x0005,startx);
	 Init_Data(0x0006,starty >> 8);
	 Init_Data(0x0007,starty);
	 Init_Data(0x0008,starty >> 8);
	 Init_Data(0x0009,starty);
	 WriteI(0x0022);
 
	 PORT_RS =1; //sbi(RS_PORT, RS);
	 PORT_NCS = 0 ; // cbi(NCS_PORT, NCS);
	 PORT_LB =  (u08)(color & 0x00FF);
	 PORT_HB = (u08)(color >> 8); 
	
	 PORT_NWR =0; //cbi(NWR_PORT, NWR);
	 PORT_NWR =1; //sbi(NWR_PORT, NWR);
	
	 PORT_NCS = 1; //sbi(NCS_PORT, NCS);
}